﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Clipper.Model;

namespace Communication
{
    /// <summary>
    /// 针
    /// </summary>
    public class ArmControl : ControlBase
    {
        string mN, mA, mD, mR;
        const string DI = "4DI";//吸液量，修改步数
        const string AM = "3AM";//定位减速度
        string mInOpen, mInClose, mOutOpen, mOutClose, mIsolatingOpen, mIsolatingClose;
        string mXy, mJy, mQx;
        
        int mTimeCount = 0;
        int mClearCanalTime = 0;
        System.Timers.Timer mTimerRinse = null;//清洗计时器
        System.Timers.Timer mTimerClearCanal = null;//管路清洗计时器
        /// <summary>
        /// 构造函数，设定命令
        /// </summary>
        /// <param name="N">复位</param>
        /// <param name="A">吸液</param>
        /// <param name="D">加液</param>
        /// <param name="R">清洗</param>
        public ArmControl(short CanId,short subCanId,string N, string A, string D, string R)
        {
            mSCmd = new Dictionary<int, string>();
            mCanId = CanId;
            mN = N;
            mA = A;
            mD = D;
            mR = R;
            mSubCanId = subCanId;

            mSCmd.Add(0, mN);
            mSCmd.Add(1,mA);
            mSCmd.Add(2,mD);
            mSCmd.Add(3,mR);

            //Can.SendCan(mSubCanId, "1GH" + 3200 + "\r");
            //Can.SendCan(mSubCanId, "1GE" + 2500 + "\r");//加液
            //Can.SendCan(mSubCanId, "1GF" + 3000 + "\r");//清洗
            //Can.SendCan(mSubCanId, "1GI" + 9000 + Can.strEnd);//洗液下降距离

            //Can.SendCan(mSubCanId, "1GE" + 1300 + "\r");
            //Can.SendCan(mSubCanId, "1GF" + 2000 + "\r");

            //Can.SendCan(mSubCanId, "1GE" + 500 + "\r");
            //Can.SendCan(mSubCanId, "1GF" + 500 + "\r");
        }
        /// <summary>
        /// 初始化参数
        /// </summary>
        /// <param name="inNumber"></param>
        /// <param name="outNumber"></param>
        /// <param name="isloating"></param>
        public void Init(int inNumber, int outNumber, int isloating,int xy,int jy,int qx)
        {
            mInOpen = "MA" + inNumber;
            mInClose = "MW" + inNumber;
            mOutOpen = "MA" + outNumber;
            mOutClose = "MW" + outNumber;
            mIsolatingOpen = "MA" + isloating;
            mIsolatingClose = "MW" + isloating;
            mXy =AM+ xy;
            mJy =AM+ jy;
            mQx =AM+ qx;  
        }
        /// <summary>
        /// 复位
        /// </summary>
        public int Reset()
        {
            SendCmd(0, mN);
            mStatus = -100;
            return 0;
        }
        /// <summary>
        /// 吸液
        /// </summary>
        public int Imbibe()
        {
            Can.SendCan(mSubCanId, mXy + Can.strEnd);
            Can.SendCan(mSubCanId, "3GD" + 3110 + "\r");//调试
            SendCmd(1, mA);
            mStatus = -1;
            return 1;
        }

        /// <summary>
        /// 吸液
        /// </summary>
        /// <param name="count">吸液量</param>
        /// <returns>1</returns>
        public int Imbibe(int count)
        {
            Can.SendCan(mSubCanId, mXy + Can.strEnd);
            Can.SendCan(mSubCanId, "3GD" + count + "\r");//调试
            SendCmd(1, mA);
            mStatus = -1;
            return 1;
        }
        /// <summary>
        /// 加液
        /// </summary>
        public int Inject()
        {
            Can.SendCan(mSubCanId, mJy + Can.strEnd);
            SendCmd(2, mD);
            mStatus = -2;
            return 2;
        }
        /// <summary>
        /// 清洗
        /// </summary>
        public int Rinse()
        {
            Can.SendCan(mSubCanId, mQx + Can.strEnd);
            SendCmd(3, mR);
            mStatus = -3;
            return 3;
        }
        /// <summary>
        /// 清洗
        /// </summary>
        public void WaitRinseTime()
        {
            WaitStatus(3);
            InOpen(true);
            OutOpen(true);
            IsolatingOpen(true);

            mTimerRinse = new System.Timers.Timer();
            mTimerRinse.Interval = 1000;
            mTimerRinse.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            mTimerRinse.Start();
            //mStatus = -3;
        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            mTimeCount++;
            //if (mTimeCount % 2 != 0)
            //{
            //    InOpen(false);
            //    OutOpen(false);
            //    IsolatingOpen(false);
            //}
            //else
            //{
                InOpen(true);
                OutOpen(true);
                IsolatingOpen(true);
            //}
            if (mTimeCount>=2)
            {
                //RinseInEnd(false); 
                mTimeCount = 0;
                mTimerRinse.Stop();
                InOpen(false);
                OutOpen(false);
                IsolatingOpen(false);
               
                ControlStatus = ControlStatusEnum.IDLE;
            }
           
        }
        /// <summary>
        /// 管路清洗，清洗完成后复位
        /// </summary>
        public void WaitClearCanal()
        {
            WaitStatus(3);
            InOpen(true);
            OutOpen(true);
            IsolatingOpen(true);

            mTimerClearCanal = new System.Timers.Timer();
            mTimerClearCanal.Interval = 1000;
            mTimerClearCanal.Elapsed += new System.Timers.ElapsedEventHandler(mTimerClearCanal_Elapsed);
            mTimerClearCanal.Start();
        }
        void mTimerClearCanal_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            mClearCanalTime++;

            //if (mClearCanalTime == 10)
            //{
            //    InOpen(false);
            //    OutOpen(false);
            //    IsolatingOpen(false);
            //}
            //else if (mClearCanalTime == 11)
            //{
            //    mClearCanalTime = 0;
            //    mTimerClearCanal.Stop();
            //    ControlStatus = ControlStatusEnum.IDLE;
            //    Reset();
            //}

            if (mClearCanalTime % 2 != 0)
            {
                InOpen(false);
                OutOpen(false);
                IsolatingOpen(false);
            }
            else
            {
                InOpen(true);
                OutOpen(true);
                IsolatingOpen(true);
            }
            if (mClearCanalTime >= 15)
            {
                InOpen(false);
                OutOpen(false);
                IsolatingOpen(false);
                mTimerClearCanal.Stop();
            }
        }

        public override void WaitStatus(int status)
        {
            base.WaitStatus(status);
            //for (int i = 0; i < 3;i++ )
            //{
                
            //}
        }
        /// <summary>
        /// 结束清洗进水,排水可以多持续一会儿
        /// </summary>
        /// <param name="isEndOut">是否结束排水</param>
        public void RinseInEnd(bool isEndOut)
        {
            OutOpen(isEndOut);
            InOpen(false);
            IsolatingOpen(false);
            mTimeCount = 0;
            if (mTimerRinse != null)
            {
                mTimerRinse.Stop();
            }

        }
        /// <summary>
        /// 进水开关
        /// </summary>
        /// <param name="open"></param>
        public void InOpen(bool open)
        {
            if (open)
                Can.SendCan(Can.IO_CanID, mInOpen + Can.strEnd);
            else
                Can.SendCan(Can.IO_CanID, mInClose + Can.strEnd);
        }
        /// <summary>
        /// 吸水开关
        /// </summary>
        /// <param name="open"></param>
        public void OutOpen(bool open)
        {
            if(open)
                Can.SendCan(Can.IO_CanID, mOutOpen + Can.strEnd);
            else
                Can.SendCan(Can.IO_CanID, mOutClose + Can.strEnd);
        }
        /// <summary>
        /// 隔离阀开关，开时吸样本
        /// </summary>
        /// <param name="open"></param>
        public void IsolatingOpen(bool open)
        {
            if (open)
                Can.SendCan(Can.IO_CanID, mIsolatingOpen + Can.strEnd);
            else
                Can.SendCan(Can.IO_CanID, mIsolatingClose + Can.strEnd);
        }

       
        /// <summary>
        /// 总阀关
        /// </summary>
        public void MainOpen()
        {
            Can.SendCan(Can.IO_CanID, "MA4\r");
            Can.SendCan(Can.IO_CanID, "MA16\r");
        }
        /// <summary>
        /// 总阀关
        /// </summary>
        public void MainClose()
        {
            Can.SendCan(Can.IO_CanID, "MW4\r");
            Can.SendCan(Can.IO_CanID, "MW16\r");
        }

    }
}
